<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/09/20 23:46:58 $)
// $RCSfile: functions_userlookup.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Searches the user & contact database and builds the tree
*/
function searchUsers($query = "")
{
	global $dbCore, $_SWIFT, $module;

	$_userlist = $_useridlist = array();
	if (!empty($query))
	{
		$dbCore->query("SELECT `userid` FROM `". TABLE_PREFIX ."users` WHERE 1 AND ". buildSQLSearch("fullname", $query) .";");
		while ($dbCore->nextRecord())
		{
			$_useridlist[] = $dbCore->Record["userid"];
		}

		$dbCore->query("SELECT `userid` FROM `". TABLE_PREFIX ."useremails` WHERE 1 AND ". buildSQLSearch("email", $query) .";");
		while ($dbCore->nextRecord())
		{
			if (!in_array($dbCore->Record["userid"], $_useridlist))
			{
				$_useridlist[] = $dbCore->Record["userid"];
			}
		}

		// Ok, now build the user list
		$dbCore->query("SELECT users.userid, users.fullname FROM `". TABLE_PREFIX ."users` AS users WHERE users.userid IN (". buildIN($_useridlist) .");");
		while ($dbCore->nextRecord())
		{
			$_userlist[$dbCore->Record["userid"]] = $dbCore->Record;
			$_userlist[$dbCore->Record["userid"]]["emails"] = array();
		}

		$dbCore->query("SELECT useremails.userid, useremails.email FROM `". TABLE_PREFIX ."useremails` AS useremails WHERE useremails.userid IN (". buildIN($_useridlist) .");");
		while ($dbCore->nextRecord())
		{
			$_userlist[$dbCore->Record["userid"]]["emails"][] = $dbCore->Record["email"];
		}
	}

	$menu = new HTML_TreeMenu();
	$usersnode = &$menu->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["users"], 'icon' => "icon_user.gif", 'expandedIcon' => "icon_user.gif", 'expanded' => true)));
	foreach ($_userlist as $key=>$val)
	{
		$emailcount = count($val["emails"]);
		if ($emailcount > 1)
		{
			$usericon = 'icon_redbigdot.gif';
		} else {
			$usericon = 'icon_yellowbigdot.gif';
		}
		$_usernode = &$usersnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". addslashes($val["fullname"]) ."&nbsp;". iif($emailcount>1, '('.intval($emailcount).')'), 'link' => "javascript:windowPlaceEmail(\'". addslashes($val["emails"][0]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => $usericon, 'expandedIcon' => $usericon, 'expanded' => true)));

		if ($emailcount > 0)
		{
			foreach ($val["emails"] as $emailkey=>$emailval)
			{
				$_emailnode = &$_usernode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". addslashes($emailval), 'link' => "javascript:windowPlaceEmail(\'". addslashes($emailval) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_msgsmallyellow.gif", 'expandedIcon' => "icon_msgsmallyellow.gif", 'expanded' => true)));
			}
		}
	}

	if ($module->isRegistered(MODULE_TEAMWORK))
	{
		$contactsnode = &$menu->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["contacts"], 'icon' => "icon_contacts.gif", 'expandedIcon' => "icon_contacts.gif", 'expanded' => true)));
		$_contactlist = array();

		if (!empty($query))
		{
			$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."contacts` WHERE ((`contacttype` = '". SWIFTPUBLIC ."') OR (`contacttype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND ((". buildSQLSearch("fullname", $query) .") OR (". buildSQLSearch("customerid", $query) .") OR (". buildSQLSearch("email1address", $query) .") OR (". buildSQLSearch("email2address", $query) .") OR (". buildSQLSearch("email3address", $query) .") OR (". buildSQLSearch("companyname", $query) .") OR (". buildSQLSearch("department", $query) .") OR (". buildSQLSearch("businessaddress", $query) .") OR (". buildSQLSearch("mobiletelephonenumber", $query) .") OR (". buildSQLSearch("businesstelephonenumber", $query) .") OR (". buildSQLSearch("hometelephonenumber", $query) ."));");
			while ($dbCore->nextRecord())
			{
				$_contactlist[$dbCore->Record["contactid"]] = $dbCore->Record;
				$_contactlist[$dbCore->Record["contactid"]]["emails"] = array();
				$_contact = &$_contactlist[$dbCore->Record["contactid"]];
				if (!empty($dbCore->Record["email1address"]))
				{
					$_contact["emails"][] = $dbCore->Record["email1address"];
				}
				if (!empty($dbCore->Record["email2address"]))
				{
					$_contact["emails"][] = $dbCore->Record["email2address"];
				}
				if (!empty($dbCore->Record["email3address"]))
				{
					$_contact["emails"][] = $dbCore->Record["email3address"];
				}

				$_emailcount = count($_contact["emails"]);

				$_contactnode = &$contactsnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". addslashes($dbCore->Record["fullname"]) ."&nbsp;". iif($_emailcount>1, '('.intval($_emailcount).')'), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["emails"][0]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => 'icon_yellowbigdot.gif', 'expandedIcon' => 'icon_yellowbigdot.gif', 'expanded' => true)));

				if ($_emailcount > 0)
				{
					foreach ($_contact["emails"] as $emailkey=>$emailval)
					{
						$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". addslashes($emailval), 'link' => "javascript:windowPlaceEmail(\'". addslashes($emailval) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_msgsmallyellow.gif", 'expandedIcon' => "icon_msgsmallyellow.gif", 'expanded' => true)));
					}
				}

				if (!empty($_contact["mobiletelephonenumber"]))
				{
					$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["c_mobile"].addslashes($_contact["mobiletelephonenumber"]), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["mobiletelephonenumber"]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_ticketphone.gif", 'expandedIcon' => "icon_ticketphone.gif", 'expanded' => true)));
				}
				if (!empty($_contact["businesstelephonenumber"]))
				{
					$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["c_business"].addslashes($_contact["businesstelephonenumber"]), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["businesstelephonenumber"]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_ticketphone.gif", 'expandedIcon' => "icon_ticketphone.gif", 'expanded' => true)));
				}
				if (!empty($_contact["business2telephonenumber"]))
				{
					$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["c_business"].addslashes($_contact["business2telephonenumber"]), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["business2telephonenumber"]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_ticketphone.gif", 'expandedIcon' => "icon_ticketphone.gif", 'expanded' => true)));
				}
				if (!empty($_contact["hometelephonenumber"]))
				{
					$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["c_home"].addslashes($_contact["hometelephonenumber"]), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["hometelephonenumber"]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_ticketphone.gif", 'expandedIcon' => "icon_ticketphone.gif", 'expanded' => true)));
				}
				if (!empty($_contact["home2telephonenumber"]))
				{
					$_emailnode = &$_contactnode->addItem(new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["c_home"].addslashes($_contact["home2telephonenumber"]), 'link' => "javascript:windowPlaceEmail(\'". addslashes($_contact["home2telephonenumber"]) ."\', \'".addslashes($_GET["formname"])."\', \'". addslashes($_GET["formfield"]) ."\')", 'icon' => "icon_ticketphone.gif", 'expandedIcon' => "icon_ticketphone.gif", 'expanded' => true)));
				}
			}
		}
	}

	$treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => stripTrailingSlash($_SWIFT["themepath"]), 'defaultClass' => 'treeMenuDefault'));
	ob_start();
	$treeMenu->printMenu(array(), true, "searchdiv");
	$contents = ob_get_contents();
	ob_end_clean();

	return $contents;
}
?>